{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Filter Restaurants by Vegan-Friendly, Price and Distance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: filterRestaurants"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #餐厅过滤器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个餐馆信息数组&nbsp;<code>restaurants</code>，其中&nbsp;&nbsp;<code>restaurants[i] = [id<sub>i</sub>, rating<sub>i</sub>, veganFriendly<sub>i</sub>, price<sub>i</sub>, distance<sub>i</sub>]</code>。你必须使用以下三个过滤器来过滤这些餐馆信息。</p>\n",
    "\n",
    "<p>其中素食者友好过滤器&nbsp;<code>veganFriendly</code>&nbsp;的值可以为 <code>true</code> 或者 <code>false</code>，如果为 <em>true</em>&nbsp;就意味着你应该只包括&nbsp;<code>veganFriendly<sub>i</sub></code>&nbsp;为 true 的餐馆，为&nbsp;<em>false</em>&nbsp;则意味着可以包括任何餐馆。此外，我们还有最大价格&nbsp;<code>maxPrice</code>&nbsp;和最大距离&nbsp;<code>maxDistance</code>&nbsp;两个过滤器，它们分别考虑餐厅的价格因素和距离因素的最大值。</p>\n",
    "\n",
    "<p>过滤后返回餐馆的 <strong><em>id</em></strong>，按照 <em><strong>rating</strong></em>&nbsp;从高到低排序。如果 <em><strong>rating</strong></em> 相同，那么按 <em><strong>id</strong></em> 从高到低排序。简单起见，&nbsp;<code>veganFriendly<sub>i</sub></code> 和 <code>veganFriendly</code>&nbsp;为&nbsp;<em>true</em>&nbsp;时取值为 <em>1</em>，为 <em>false</em> 时，取值为&nbsp;<em>0 。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10\n",
    "<strong>输出：</strong>[3,1,5] \n",
    "<strong>解释： \n",
    "</strong>这些餐馆为：\n",
    "餐馆 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]\n",
    "餐馆 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]\n",
    "餐馆 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]\n",
    "餐馆 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]\n",
    "餐馆 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] \n",
    "在按照 veganFriendly = 1, maxPrice = 50 和 maxDistance = 10 进行过滤后，我们得到了餐馆 3, 餐馆 1 和 餐馆 5（按评分从高到低排序）。 \n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10\n",
    "<strong>输出：</strong>[4,3,2,1,5]\n",
    "<strong>解释：</strong>餐馆与示例 1 相同，但在 veganFriendly = 0 的过滤条件下，应该考虑所有餐馆。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3\n",
    "<strong>输出：</strong>[4,5]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;=&nbsp;restaurants.length &lt;= 10^4</code></li>\n",
    "\t<li><code>restaurants[i].length == 5</code></li>\n",
    "\t<li><code>1 &lt;=&nbsp;id<sub>i</sub>, rating<sub>i</sub>, price<sub>i</sub>, distance<sub>i </sub>&lt;= 10^5</code></li>\n",
    "\t<li><code>1 &lt;=&nbsp;maxPrice,&nbsp;maxDistance &lt;= 10^5</code></li>\n",
    "\t<li><code>veganFriendly<sub>i</sub></code> 和&nbsp;<code>veganFriendly</code>&nbsp;的值为 0 或 1 。</li>\n",
    "\t<li>所有 <code>id<sub>i</sub></code> 各不相同。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [filter-restaurants-by-vegan-friendly-price-and-distance](https://leetcode.cn/problems/filter-restaurants-by-vegan-friendly-price-and-distance/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [filter-restaurants-by-vegan-friendly-price-and-distance](https://leetcode.cn/problems/filter-restaurants-by-vegan-friendly-price-and-distance/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]]\\n1\\n50\\n10', '[[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]]\\n0\\n50\\n10', '[[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]]\\n0\\n30\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        pass_restaurants = []\n",
    "        while restaurants:\n",
    "            restaurant = restaurants.pop()\n",
    "            if veganFriendly > restaurant[2] or maxPrice < restaurant[3] or maxDistance < restaurant[4]:\n",
    "                pass\n",
    "            else:\n",
    "                pass_restaurants.append(restaurant)\n",
    "        pass_restaurants = sorted(pass_restaurants, key = lambda x: (x[1], x[0]), reverse=True)\n",
    "        list_res = [i[0] for i in pass_restaurants]\n",
    "        return list_res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ans = []\n",
    "        if veganFriendly == 1:\n",
    "            for x in restaurants:\n",
    "                if x[2] == 1 and x[3] <= maxPrice and x[4] <= maxDistance:\n",
    "                    ans.append(x)\n",
    "        else:\n",
    "            for x in restaurants:\n",
    "                if x[3] <= maxPrice and x[4] <= maxDistance:\n",
    "                    ans.append(x)\n",
    "\n",
    "        ans.sort(key = lambda x: (-x[1], -x[0]))\n",
    "        return [x[0] for x in ans]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        pass_restaurants = []\n",
    "        while restaurants:\n",
    "            restaurant = restaurants.pop()\n",
    "            if veganFriendly > restaurant[2] or maxPrice < restaurant[3] or maxDistance < restaurant[4]:\n",
    "                pass\n",
    "            else:\n",
    "                pass_restaurants.append(restaurant)\n",
    "        pass_restaurants = sorted(pass_restaurants, key = lambda x: (x[1], x[0]), reverse=True)\n",
    "        list_res = [i[0] for i in pass_restaurants]\n",
    "        return list_res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        pass_restaurants = []\n",
    "        while restaurants:\n",
    "            restaurant = restaurants.pop()\n",
    "            if veganFriendly > restaurant[2] or maxPrice < restaurant[3] or maxDistance < restaurant[4]:\n",
    "                pass\n",
    "            else:\n",
    "                pass_restaurants.append(restaurant)\n",
    "        pass_restaurants = sorted(pass_restaurants, key = lambda x: (x[1], x[0]), reverse=True)\n",
    "        list_res = [i[0] for i in pass_restaurants]\n",
    "        return list_res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        r = []\n",
    "        for i in restaurants :\n",
    "            if i[3] > maxPrice or i[4] > maxDistance :\n",
    "                r += [i]\n",
    "            elif veganFriendly == 1 and i[2] == 0:\n",
    "                r += [i]\n",
    "        for i in r :\n",
    "            restaurants.remove(i)\n",
    "\n",
    "        r = []\n",
    "        n = len(restaurants)\n",
    "        for i in range(n):\n",
    "            restaurants[i] = restaurants[i][1::-1]\n",
    "        restaurants.sort(reverse=True)\n",
    "        for i in restaurants:\n",
    "            r += [i[1]]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        \n",
    "        for i in reversed(range(len(restaurants))):\n",
    "            if restaurants[i][3]>maxPrice or restaurants[i][4]>maxDistance or(veganFriendly==1 and restaurants[i][2]==0):\n",
    "                del restaurants[i]\n",
    "        for i in range(len(restaurants)):\n",
    "            restaurants[i] = [restaurants[i][1],restaurants[i][0]]\n",
    "        restaurants.sort(reverse=True)\n",
    "        ans = [0]*len(restaurants)\n",
    "        for i in range(len(restaurants)):\n",
    "            ans[i] = restaurants[i][1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for i in reversed(range(len(restaurants))):\n",
    "            if restaurants[i][3]>maxPrice or restaurants[i][4]>maxDistance or (veganFriendly==1 and restaurants[i][2]==0):\n",
    "                del restaurants[i]\n",
    "        for i in range(len(restaurants)):\n",
    "            restaurants[i] = [restaurants[i][1],restaurants[i][0]]\n",
    "        restaurants.sort(reverse=True)\n",
    "        \n",
    "        return [restaurants[i][1] for i in range(len(restaurants))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        i = 0\n",
    "        while i < len(restaurants):\n",
    "            if (veganFriendly == 1 and restaurants[i][2] != veganFriendly) or restaurants[i][3] > maxPrice or restaurants[i][4] > maxDistance:\n",
    "                restaurants.pop(i)\n",
    "            else:\n",
    "                i += 1\n",
    "        restaurants.sort()\n",
    "        restaurants.sort(key=lambda x:x[1])\n",
    "        return [i[0] for i in restaurants[::-1]]\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        r = []\n",
    "        if veganFriendly == 1 :\n",
    "            for i in restaurants :\n",
    "                if i[2] == 0:\n",
    "                    r += [i]\n",
    "        for i in r :\n",
    "            restaurants.remove(i)\n",
    "\n",
    "        r = []\n",
    "        for i in restaurants :\n",
    "            if i[3] > maxPrice or i[4] > maxDistance :\n",
    "                r += [i]\n",
    "        for i in r :\n",
    "            restaurants.remove(i)\n",
    "\n",
    "        r = []\n",
    "        n = len(restaurants)\n",
    "        for i in range(n):\n",
    "            restaurants[i] = restaurants[i][1::-1]\n",
    "        restaurants.sort(reverse=True)\n",
    "        for i in restaurants:\n",
    "            r += [i[1]]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for i in range(len(restaurants)-1, -1, -1):\n",
    "            if (veganFriendly == 1 and restaurants[i][2] == 0) or (restaurants[i][3] > maxPrice) or (restaurants[i][4] > maxDistance):\n",
    "                del restaurants[i]\n",
    "        restaurants = sorted(restaurants, key = lambda x:(-x[1], -x[0]))\n",
    "        return [i[0] for i in restaurants]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        n = len(restaurants)\n",
    "        for i in range(n):\n",
    "            if veganFriendly==1:\n",
    "                if restaurants[i][2]==0:\n",
    "                    restaurants[i]=[]\n",
    "                    continue\n",
    "            if restaurants[i][3]>maxPrice:\n",
    "                restaurants[i]=[]\n",
    "                continue\n",
    "            if restaurants[i][4]>maxDistance:\n",
    "                restaurants[i]=[]\n",
    "                continue\n",
    "        while [] in restaurants:\n",
    "            restaurants.remove([])\n",
    "        alist = sorted(restaurants,key=lambda x:(x[1],x[0]),reverse = True)\n",
    "        ans = []\n",
    "        for i in alist:\n",
    "            ans.append(i[0])\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: list[list[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> list[int]:\n",
    "        i = 0\n",
    "        while i < len(restaurants):\n",
    "            if restaurants[i][2] < veganFriendly or restaurants[i][3] > maxPrice or restaurants[i][4] > maxDistance:\n",
    "                del restaurants[i]\n",
    "                continue\n",
    "            i += 1\n",
    "        restaurants.sort(key = lambda x:float(x[1]) + x[0]/1000000, reverse=True)\n",
    "        return [i[0] for i in restaurants]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        que = []\n",
    "        res = []\n",
    "        while restaurants:\n",
    "            idi, ratingi, veganFriendlyi, pricei, distancei = restaurants.pop()\n",
    "            if pricei<= maxPrice and distancei <= maxDistance:\n",
    "                if veganFriendly == 0 or veganFriendlyi == veganFriendly:\n",
    "                    heapq.heappush(que,[-ratingi,-idi])\n",
    "        #print(que)\n",
    "        while que:\n",
    "            rat,idex = heapq.heappop(que)\n",
    "            res.append(-idex)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) ->List[int]:\n",
    "        for i in range(len(restaurants) - 1, -1, -1):\n",
    "            _, ratingi, veganFriendlyi, pricei, distancei = restaurants[i]\n",
    "            if (veganFriendly == 1 and veganFriendlyi == 0) or (pricei > maxPrice) or distancei > maxDistance:\n",
    "                restaurants.pop(i)\n",
    "        restaurants = sorted(restaurants,key=lambda x:x[0],reverse=True)\n",
    "        restaurants = sorted(restaurants,key=lambda x:x[1],reverse=True)\n",
    "\n",
    "\n",
    "        return [item[0] for item in restaurants]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "\n",
    "        for i in range(len(restaurants)-1, -1, -1):\n",
    "            if restaurants[i][2] < veganFriendly or restaurants[i][3] > maxPrice or restaurants[i][4] > maxDistance:\n",
    "                del restaurants[i]\n",
    "        \n",
    "        restaurants.sort(key=lambda r: (-r[1], -r[0]))\n",
    "        return [i[0] for i in restaurants]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for i in range(len(restaurants) - 1, -1, -1):\n",
    "            restaurant = restaurants[i]\n",
    "            if veganFriendly and restaurant[2] == 0:\n",
    "                restaurants.pop(i)\n",
    "                continue\n",
    "            if restaurant[3] > maxPrice:\n",
    "                restaurants.pop(i)\n",
    "                continue\n",
    "            if restaurant[4] > maxDistance:\n",
    "                restaurants.pop(i)\n",
    "        for i in range(len(restaurants)-1):\n",
    "            for j in range(i+1, len(restaurants)):\n",
    "                if restaurants[i][1] < restaurants[j][1]:\n",
    "                    restaurants[i], restaurants[j] = restaurants[j],restaurants[i]\n",
    "                if restaurants[i][1] == restaurants[j][1] and restaurants[i][0] < restaurants[j][0]:\n",
    "                    restaurants[i], restaurants[j] = restaurants[j],restaurants[i]\n",
    "\n",
    "        return [restaurant[0] for restaurant in restaurants]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        n = len(restaurants)\n",
    "        i=0\n",
    "        while i<len(restaurants):\n",
    "            idx,r,v,p,d = restaurants[i]\n",
    "            if (veganFriendly==1 and v!=1) or p>maxPrice or d>maxDistance:\n",
    "                del restaurants[i]\n",
    "            else:\n",
    "                i+=1\n",
    "        restaurants.sort(key=lambda a:(-a[1],-a[0]))\n",
    "        return [a[0] for a in restaurants]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        rating_list=restaurants\n",
    "        i=0\n",
    "        while i<len(rating_list):\n",
    "            if veganFriendly==1 and rating_list[i][2]==0:\n",
    "                rating_list.pop(i)\n",
    "                continue\n",
    "            elif rating_list[i][3]>maxPrice:\n",
    "                rating_list.pop(i)\n",
    "                continue\n",
    "            elif rating_list[i][4]>maxDistance:\n",
    "                rating_list.pop(i)\n",
    "                continue\n",
    "            i+=1\n",
    "            \n",
    "        print(rating_list)\n",
    "        res_dict={}\n",
    "        for i in range(len(rating_list)):\n",
    "            if rating_list[i][1] not in res_dict:\n",
    "                res_dict[rating_list[i][1]]=[rating_list[i][0]]\n",
    "            else:\n",
    "                res_dict[rating_list[i][1]].append(rating_list[i][0])\n",
    "        print(res_dict)\n",
    "        res=[]\n",
    "        key_value=sorted(res_dict.keys(),reverse=True)\n",
    "        for i in range(len(key_value)):\n",
    "            print(res_dict[key_value[i]])\n",
    "            res+=sorted(res_dict[key_value[i]],reverse=True)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:   \n",
    "        for restaurant in restaurants[::-1]:\n",
    "            if restaurant[3] > maxPrice:\n",
    "                restaurants.remove(restaurant)\n",
    "            elif restaurant[4] > maxDistance:\n",
    "                restaurants.remove(restaurant)\n",
    "            elif veganFriendly and not restaurant[2]:\n",
    "                restaurants.remove(restaurant)\n",
    "        \n",
    "        restaurants.sort(key=lambda x:(x[1],x[0]), reverse=True)\n",
    "        ans = []\n",
    "        for res in restaurants:\n",
    "            ans.append(res[0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        i = 0\n",
    "        while i < len(restaurants):\n",
    "            if veganFriendly:\n",
    "                if restaurants[i][2]==0:\n",
    "                    del(restaurants[i])\n",
    "                    continue\n",
    "            if restaurants[i][3] > maxPrice or restaurants[i][4] > maxDistance:\n",
    "                del(restaurants[i])\n",
    "                continue\n",
    "            i += 1\n",
    "        restaurants.sort(key=lambda x:(-x[1],-x[0]))\n",
    "        return [rest[0] for rest in restaurants]\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        result = []\n",
    "        if veganFriendly == 1:\n",
    "            i = 0\n",
    "            while i < len(restaurants):\n",
    "                if restaurants[i][2]==0:\n",
    "                    del restaurants[i]\n",
    "                    i -=1\n",
    "                i+=1\n",
    "        i = 0\n",
    "        while i < len(restaurants):\n",
    "            if restaurants[i][3]>maxPrice or restaurants[i][4]>maxDistance:\n",
    "                    del restaurants[i]\n",
    "                    i -=1\n",
    "            i+=1\n",
    "        restaurants.sort(key=lambda restaurants: (-restaurants[1], -restaurants[0]))\n",
    "        for i in range(len(restaurants)):\n",
    "            result.append(restaurants[i][0])\n",
    "        return result\n",
    "\n",
    "    def getSecond(elem):\n",
    "        return elem[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for i in range(len(restaurants) - 1, -1, -1):\n",
    "            if veganFriendly:\n",
    "                if restaurants[i][2] == 0:\n",
    "                    restaurants.pop(i)\n",
    "                    continue\n",
    "            if restaurants[i][3] > maxPrice:\n",
    "                restaurants.pop(i)\n",
    "                continue\n",
    "            if restaurants[i][4] > maxDistance:\n",
    "                restaurants.pop(i)\n",
    "        restaurants.sort(key = lambda x: (x[1], x[0]), reverse = True)\n",
    "        res = []\n",
    "        for i in restaurants:\n",
    "            res.append(i[0])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        i = 0\n",
    "        while i < len(restaurants):\n",
    "            if veganFriendly == 1:\n",
    "                if restaurants[i][2] == 0:\n",
    "                    restaurants.remove(restaurants[i])\n",
    "                    continue\n",
    "            if restaurants[i][3] > maxPrice:\n",
    "                restaurants.remove(restaurants[i])\n",
    "                continue\n",
    "            if restaurants[i][4] > maxDistance:\n",
    "                restaurants.remove(restaurants[i])\n",
    "                continue\n",
    "            i += 1\n",
    "        restaurants.sort(key=lambda x: (-x[1], -x[0]))\n",
    "        ans = [x[0] for x in restaurants]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        l = len(restaurants)\n",
    "\n",
    "        for i, n in enumerate(restaurants[::-1]):\n",
    "            if veganFriendly == 1 and n[2] == 0:\n",
    "                del restaurants[l-i-1]\n",
    "            elif n[3] > maxPrice:\n",
    "                del restaurants[l-i-1]\n",
    "            elif n[4] > maxDistance:\n",
    "                del restaurants[l-i-1]\n",
    "        \n",
    "        restaurants.sort(key=lambda x:(x[1], x[0]), reverse=True)\n",
    "        print(restaurants)\n",
    "\n",
    "        return [e[0] for e in restaurants]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        #遍历对数据进行过滤\n",
    "        flag = []\n",
    "        for i in range(len(restaurants)):\n",
    "            #首先过滤素食主义者信息\n",
    "            if veganFriendly == 1:\n",
    "                if restaurants[i][2] == 0:\n",
    "                    flag.append(i)\n",
    "                    continue\n",
    "            if restaurants[i][3] > maxPrice:\n",
    "                flag.append(i)\n",
    "                continue\n",
    "            if restaurants[i][4] > maxDistance:\n",
    "                flag.append(i)\n",
    "                continue\n",
    "        #flag是我们要过滤掉的数据的索引列表\n",
    "        # 进行过滤\n",
    "        for i in sorted(flag, reverse=True):\n",
    "            del restaurants[i]\n",
    "        restaurants = sorted(restaurants, key=lambda x: (-x[1], -x[0]))\n",
    "        answer = []\n",
    "        for i in range(len(restaurants)):\n",
    "            answer.append(restaurants[i][0])\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1] = 0\n",
    "            elif maxPrice < rest[3] or maxDistance < rest[4]:\n",
    "                rest[1] = 0\n",
    "\n",
    "        restaurants.sort(key = lambda x:[x[1],x[0]], reverse=True)\n",
    "        result = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                result.append(rest[0])\n",
    "                \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        restaurants = sorted(restaurants, key=lambda x:(x[1], x[0]), reverse=True)\n",
    "        result = []\n",
    "        for data in restaurants:\n",
    "            if data[3] <= maxPrice and data[4] <= maxDistance:\n",
    "                if veganFriendly:\n",
    "                    if data[2]:\n",
    "                        result.append(data)\n",
    "                else:\n",
    "                    result.append(data)\n",
    "        return [i[0] for i in result]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        result = list()\n",
    "        # 输入参数合法性判断\n",
    "        if restaurants is None or not isinstance(restaurants, list):  # restaurants不为None，必须为list的实例\n",
    "            return result\n",
    "        length = len(restaurants)\n",
    "        if length < 1 or length > 10000:  # 1 <= restaurants.length <= 10^4\n",
    "            return result\n",
    "        if veganFriendly == 0 or veganFriendly == 1:  # veganFriendly 的值为 0 或 1\n",
    "            pass\n",
    "        else:\n",
    "            return result\n",
    "        if maxPrice < 1 or maxPrice > 100000:   # 1 <= maxPrice <= 10^5\n",
    "            return result\n",
    "        if maxDistance < 1 or maxDistance > 100000:   # 1 <= maxDistance <= 10^5\n",
    "            return result\n",
    "\n",
    "        # 过滤掉不满足条件的餐厅\n",
    "        number = 0\n",
    "        for id, rating, veganFriend, price, distance in restaurants:\n",
    "            #元素合法性判断\n",
    "            if id < 1 or id > 100000:  # 1 <= id <= 10^5\n",
    "                return result\n",
    "            if rating < 1 or rating > 100000:  # 1 <= rating <= 10^5\n",
    "                return result\n",
    "            if veganFriend == 0 or veganFriend == 1:  # veganFriend 的值为 0 或 1\n",
    "                pass\n",
    "            else:\n",
    "                return result\n",
    "            if price < 1 or price > 100000:  # 1 <= price <= 10^5\n",
    "                return result\n",
    "            if distance < 1 or distance > 100000:  # 1 <= distance <= 10^5\n",
    "                return result\n",
    "\n",
    "            # 素食者友好过滤器 veganFriendly 的值可以为 true 或者 false，如果为 true 就意味着你应该只包括 veganFriendlyi 为 true 的餐馆，为 false 则意味着可以包括任何餐馆\n",
    "            if(veganFriendly == 1 and veganFriend != 1) or price > maxPrice or distance > maxDistance :\n",
    "                restaurants[number] = [0, 0]\n",
    "            else:\n",
    "                restaurants[number] = [rating, id]\n",
    "            number += 1\n",
    "\n",
    "        restaurants.sort(reverse=True)\n",
    "        if restaurants[0][1] == 0:  # 如果删选后的列表没有合格元素，就直接反回None\n",
    "            return result\n",
    "\n",
    "        for item in restaurants:\n",
    "            if item[1]:\n",
    "                result.append(item[1])\n",
    "\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        vegan_idx = set((rest[1], rest[0]) for rest in restaurants if rest[2] == 1) \\\n",
    "            if veganFriendly == 1 else set((rest[1], rest[0]) for rest in restaurants)\n",
    "        price_idx = set((rest[1], rest[0]) for rest in restaurants if rest[3] <= maxPrice)\n",
    "        dist_idx = set((rest[1], rest[0]) for rest in restaurants if rest[4] <= maxDistance)\n",
    "        res = sorted(vegan_idx & price_idx & dist_idx, reverse=True)\n",
    "        res = [idx for rating, idx in res]\n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        vege = []\n",
    "        for each in restaurants:\n",
    "            if each[2] == 1:\n",
    "                vege.append(each)\n",
    "        # print(vege)\n",
    "        if veganFriendly == 1:\n",
    "            res = sorted(vege, key=lambda k:(k[1], k[0]), reverse = True)\n",
    "        else:\n",
    "            res = sorted(restaurants, key=lambda k:(k[1], k[0]), reverse = True)\n",
    "        # print(res)\n",
    "        output = []\n",
    "        for each in res:\n",
    "            if each[3] <= maxPrice and each[4] <= maxDistance:\n",
    "                output.append(each[0])\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        if veganFriendly == 1:\n",
    "            filter_res = [i for i in restaurants if i[3] <= maxPrice and i[-1] <= maxDistance and i[2]]\n",
    "        else:\n",
    "            filter_res = [i for i in restaurants if i[3] <= maxPrice and i[-1] <= maxDistance]\n",
    "        \n",
    "        filter_res.sort(key=lambda x: (-x[1], -x[0]))\n",
    "        return [i[0] for i in filter_res]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        dic = defaultdict(int)\n",
    "        if veganFriendly == 1:\n",
    "            for restaurant in restaurants:\n",
    "                print(restaurant[0], restaurant[1])\n",
    "                if restaurant[2] == 0:\n",
    "                    continue\n",
    "                if restaurant[3] <= maxPrice and restaurant[4] <= maxDistance:\n",
    "                    dic[restaurant[0]] = restaurant[1]\n",
    "        else:\n",
    "            for restaurant in restaurants:\n",
    "                if restaurant[3] <= maxPrice and restaurant[4] <= maxDistance:\n",
    "                    dic[restaurant[0]] = restaurant[1]\n",
    "        ans = sorted(dic.items(), key = lambda x:(x[1], x[0]), reverse = True)\n",
    "        return [x[0] for x in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        temp = []\n",
    "        # 过滤\n",
    "        for id, rating, veganFriend, price, distance in restaurants:\n",
    "            if not veganFriendly or veganFriend == veganFriendly:\n",
    "                if price <= maxPrice and distance <= maxDistance:\n",
    "                    temp.append([rating, id])\n",
    "        # 排序\n",
    "        temp.sort(reverse=True)\n",
    "        res = [i[1] for i in temp]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "#         for i in range(len(restaurants)):\n",
    "#             for j in range(len(restaurants)):\n",
    "#                 if j+1 < len(restaurants) and restaurants[j][1] < restaurants[j+1][1]:\n",
    "#                     restaurants[j], restaurants[j+1] = restaurants[j+1], restaurants[j]\n",
    "#                 if j+1 < len(restaurants) and restaurants[j][1] == restaurants[j+1][1]:\n",
    "#                     if restaurants[j][0] < restaurants[j+1][0]:\n",
    "#                         restaurants[j], restaurants[j+1] = restaurants[j+1], restaurants[j]\n",
    "                \n",
    "        restaurants.sort(key=lambda x :[x[1], x[0]], reverse=True)#reverse表示是否颠倒排序\n",
    "    \n",
    "        l = []\n",
    "        if veganFriendly == 1:\n",
    "            for i in range(len(restaurants)):\n",
    "                if restaurants[i][2] == 1 and restaurants[i][3] <= maxPrice and restaurants[i][-1] <= maxDistance:\n",
    "                    l.append(restaurants[i][0])\n",
    "        else:\n",
    "            for i in range(len(restaurants)):\n",
    "                if restaurants[i][3] <= maxPrice and restaurants[i][-1] <= maxDistance:\n",
    "                    l.append(restaurants[i][0])\n",
    "                    \n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ans = []\n",
    "        for id, rating, veganFriend, price, distance in restaurants:\n",
    "            if price <= maxPrice and distance <= maxDistance:\n",
    "                if veganFriend == veganFriendly or not veganFriendly:\n",
    "                    ans.append([rating, id])\n",
    "        ans.sort(reverse=True)\n",
    "        return [item[1] for item in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        restaurants.sort(key=lambda x:(x[1], x[0]), reverse=True)\n",
    "        res = []\n",
    "        for e in restaurants:\n",
    "            if (e[2] or not veganFriendly) and e[3] <= maxPrice and e[4] <= maxDistance:\n",
    "                res.append(e[0])\n",
    "        # print(restaurants)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ans = []\n",
    "        for id,rating,veganFriend,price,distance in restaurants:\n",
    "            if price <= maxPrice and distance <= maxDistance:\n",
    "                if veganFriend == veganFriendly or not veganFriendly:\n",
    "                    ans.append([rating,id])\n",
    "        ans.sort(reverse=True)\n",
    "        return [item[1] for item in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ans = []\n",
    "        for id, rating, veganFriend, price, distance in restaurants:\n",
    "            if price <= maxPrice and distance <= maxDistance:\n",
    "                if veganFriend == veganFriendly or not veganFriendly:\n",
    "                    ans.append([rating, id])\n",
    "        ans.sort(reverse=True)\n",
    "        return [item[1] for item in ans]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        # filter: veganFridendly, <= maxp and <= maxd\n",
    "        # return id sorted by rating, id (both reversed)\n",
    "        res = []\n",
    "        for id, rating, veg, p, d in restaurants:\n",
    "            if veganFriendly == 1:\n",
    "                if veg == 0:\n",
    "                    continue\n",
    "            if p > maxPrice or d > maxDistance:\n",
    "                continue\n",
    "            res.append([id, [-rating, -id]])\n",
    "        return [x1 for x1, x2  in sorted(res, key=lambda x: x[1])]\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        temp = []\n",
    "        ans = []\n",
    "        for res in restaurants:\n",
    "            if veganFriendly == 1:\n",
    "                if res[2] == 1 and res[3] <= maxPrice and res[4] <= maxDistance:\n",
    "                    temp.append(res)\n",
    "            else:\n",
    "                if res[3] <= maxPrice and res[4] <= maxDistance:\n",
    "                    temp.append(res)\n",
    "        temp.sort(key=lambda x:(-x[1], -x[0]))\n",
    "        print(temp)\n",
    "        for id in temp:\n",
    "            ans.append(id[0])\n",
    "        return ans\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        f = [[-restaurant[1], -restaurant[0]] for restaurant in restaurants if restaurant[3] <= maxPrice and restaurant[4] <= maxDistance and (not veganFriendly or restaurant[2])]\n",
    "        f.sort()\n",
    "        return [-i[1] for i in f]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        l=[]\n",
    "        for i in restaurants:\n",
    "            if veganFriendly==1 and veganFriendly!=i[2]:\n",
    "                continue\n",
    "            elif maxPrice<i[3] or maxDistance<i[4]:\n",
    "                continue\n",
    "            l.append(i)\n",
    "        l.sort(key=lambda x:x[0],reverse=1)\n",
    "        l.sort(key=lambda x:x[1],reverse=1)\n",
    "        b=[]\n",
    "        for i in l:\n",
    "            b.append(i[0])\n",
    "        return b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sqlite3 import connect\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        db=connect(':memory:')\n",
    "        cur=db.cursor()\n",
    "        cur.execute('create table tb(id int,rating int,veganFriendly int,price int,distance int)')\n",
    "        for i in restaurants:\n",
    "            cur.execute(f\"insert into tb values {tuple(i)}\")\n",
    "        if veganFriendly:\n",
    "            cur.execute(f\"\"\"select id from tb \n",
    "            where price<={maxPrice} and distance<={maxDistance} and veganFriendly=1\n",
    "            order by rating desc,id desc\"\"\")\n",
    "        else:\n",
    "            cur.execute(f\"\"\"select id from tb \n",
    "            where price<={maxPrice} and distance<={maxDistance}\n",
    "            order by rating desc,id desc\"\"\")\n",
    "        return list(chain(*list(cur.fetchall())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        restaurants = sorted(restaurants,key = lambda x:(x[1],x[0]),reverse = True)\n",
    "        ans = []\n",
    "        for id,r,v,p,d in restaurants:\n",
    "            if veganFriendly == 0:\n",
    "               if p <= maxPrice and d <= maxDistance:\n",
    "                  ans.append(id)\n",
    "            else:\n",
    "                if p <= maxPrice and d <= maxDistance and v == veganFriendly:\n",
    "                  ans.append(id)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sqlite3 import connect\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        \n",
    "        db=connect(':memory:')\n",
    "        cur=db.cursor()\n",
    "        cur.execute('create table tb(id int,rating int,veganFriendly int,price int,distance int)')\n",
    "        for i in restaurants:cur.execute(f\"insert into tb values {tuple(i)}\")\n",
    "\n",
    "        veganFriendly = 'and veganFriendly=1' if veganFriendly else ''\n",
    "\n",
    "        cur.execute(f\"\"\"select id from tb \n",
    "        where price<={maxPrice} and distance<={maxDistance} {veganFriendly}\n",
    "        order by rating desc,id desc\"\"\")\n",
    "\n",
    "        return list(chain(*list(cur.fetchall())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sqlite3 import connect\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        db=connect(':memory:')\n",
    "        cur=db.cursor()\n",
    "        cur.execute('create table tb(id int,rating int,veganFriendly int,price int,distance int)')\n",
    "        for i in restaurants:\n",
    "            cur.execute(f\"insert into tb values {tuple(i)}\")\n",
    "\n",
    "        veganFriendly = 'and veganFriendly=1' if veganFriendly else ''\n",
    "        \n",
    "        cur.execute(f\"\"\"select id from tb \n",
    "        where price<={maxPrice} and distance<={maxDistance} {veganFriendly}\n",
    "        order by rating desc,id desc\"\"\")\n",
    "\n",
    "        return list(chain(*list(cur.fetchall())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "\n",
    "        for item in restaurants:\n",
    "            if veganFriendly:\n",
    "                if item[2]!=veganFriendly:\n",
    "                    item[1] = -1\n",
    "            if item[3] > maxPrice:\n",
    "                item[1] = -1\n",
    "            if item[4] > maxDistance:\n",
    "                item[1] = -1\n",
    "        restaurants.sort(key=lambda x:(-x[1], -x[0]))\n",
    "        r = []\n",
    "        for item in restaurants:\n",
    "            if item[1]<0:\n",
    "                break\n",
    "            r.append(item[0])\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1] = 0\n",
    "            elif maxDistance < rest[4] or maxPrice < rest[3]:\n",
    "                rest[1] = 0\n",
    "        restaurants.sort(key=lambda x: [x[1], x[0]], reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        rating = {}\n",
    "        res = []\n",
    "        for i in restaurants:\n",
    "            if veganFriendly == 1:\n",
    "                if i[2] == 0:\n",
    "                    i[1] = -1\n",
    "            if i[3] > maxPrice or i[4] > maxDistance:\n",
    "                i[1] = -1\n",
    "            if i[1] != -1:\n",
    "                rating.update({i[0]: i[1]})\n",
    "        rating = sorted(rating.items(), key=lambda x: x[1], reverse=True)\n",
    "        for j in range(len(rating)-1):\n",
    "            if rating[j][1] == rating[j+1][1] and rating[j][0] < rating[j+1][0]:\n",
    "                rating[j], rating[j+1] = rating[j+1], rating[j]\n",
    "        for j in range(len(rating)):\n",
    "            res.append(rating[j][0])\n",
    "\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1] = 0\n",
    "            elif maxPrice < rest[3] or maxDistance < rest[4]:\n",
    "                rest[1] = 0\n",
    "        restaurants.sort(key = lambda x:[x[1], x[0]], reverse = True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        #需要对素食进行筛选\n",
    "        if veganFriendly:\n",
    "            for restaurant in restaurants:\n",
    "                if restaurant[2] == 0:\n",
    "                    restaurant[1] = -1\n",
    "        # 分别对价格与距离进行筛选\n",
    "        for restaurant in restaurants:\n",
    "            if restaurant[3] > maxPrice or restaurant[4] > maxDistance:\n",
    "                restaurant[1] = -1\n",
    "        sorted_list = sorted(restaurants, key=lambda x: (x[1],x[0]),reverse=True)\n",
    "        res = []\n",
    "        for i in sorted_list:\n",
    "            if i[1] != -1:\n",
    "                res.append(i[0])\n",
    "            else:\n",
    "                break\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        for rest in restaurants:\n",
    "            if veganFriendly and not rest[2]:\n",
    "                rest[1]=0\n",
    "            elif maxPrice<rest[3] or maxDistance<rest[4]:\n",
    "                rest[1]=0\n",
    "        restaurants.sort(key=lambda x:[x[1],x[0]],reverse=True)\n",
    "        a = []\n",
    "        for rest in restaurants:\n",
    "            if rest[1]:\n",
    "                a.append(rest[0])\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    @staticmethod\n",
    "    def filterRestaurants(restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> \\\n",
    "            List[int]:\n",
    "        restaurants.sort(key=(lambda x: [x[1], x[0]]), reverse=True)\n",
    "        if veganFriendly:\n",
    "            res = [i for i in restaurants if i[2] and i[3] <= maxPrice and i[4] <= maxDistance]\n",
    "        else:\n",
    "            res = [i for i in restaurants if i[3] <= maxPrice and i[4] <= maxDistance]\n",
    "        ans = []\n",
    "        for i in res:\n",
    "            ans.append(i[0])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        res = []\n",
    "        for idi, ratingi, veganFriendlyi, pricei, distancei in restaurants:\n",
    "            if(veganFriendly == 1 and veganFriendlyi == 0):\n",
    "                continue\n",
    "            if(pricei <= maxPrice and distancei <= maxDistance):\n",
    "                res.append([idi, ratingi])\n",
    "        res.sort(key = lambda x:[-x[1],-x[0]])\n",
    "        final = []\n",
    "        for item in res:\n",
    "            final.append(item[0])\n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        l = len(restaurants)\n",
    "        restaurants_ = []\n",
    "        if veganFriendly:\n",
    "            for i in range(l):\n",
    "                if restaurants[i][2] and restaurants[i][3]<=maxPrice and restaurants[i][4]<=maxDistance:\n",
    "                    restaurants_.append(restaurants[i])\n",
    "        else:\n",
    "            for i in range(l):\n",
    "                if restaurants[i][3]<=maxPrice and restaurants[i][4]<=maxDistance:\n",
    "                    restaurants_.append(restaurants[i])\n",
    "        l_ = len(restaurants_)\n",
    "        for j in range(l_):\n",
    "            i = 0\n",
    "            while i+1 < l_-j:\n",
    "                if restaurants_[i][1]>restaurants_[i+1][1] or (restaurants_[i][1]==restaurants_[i+1][1] and restaurants_[i][0]>restaurants_[i+1][0]):\n",
    "                    restaurants_[i],restaurants_[i+1] = restaurants_[i+1],restaurants_[i]\n",
    "                i += 1\n",
    "        res = list(reversed([x[0] for x in restaurants_]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ret = []\n",
    "        restaurants.sort(key=lambda x:(-x[1],-x[0]))\n",
    "        for id,rating,v,price,dis in restaurants:\n",
    "            if v >= veganFriendly and price <= maxPrice and dis <= maxDistance:\n",
    "                ret.append(id)\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ans = [] \n",
    "        restaurants.sort(key = lambda x :(-x[1], -x[0]))\n",
    "        for idx, _ , v, price, dist in restaurants:\n",
    "            if v >= veganFriendly and price <= maxPrice and dist <= maxDistance:\n",
    "                #print(idx)\n",
    "                ans.append(idx)\n",
    "        return ans\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        result_list = []\n",
    "        range_list = []\n",
    "        for res in restaurants:\n",
    "            if res[3] <= maxPrice and res[4] <= maxDistance:\n",
    "                if veganFriendly:\n",
    "                    if res[2] == 1:\n",
    "                        result_list.append(res)\n",
    "                else:\n",
    "                    result_list.append(res)\n",
    "        rat = sorted(result_list, key=lambda x: (-x[1],-x[0]))\n",
    "        range_list = [row[0] for row in sorted(result_list, key=lambda x: (-x[1],-x[0]))]\n",
    "        return range_list\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        filter=[]\n",
    "        for i in restaurants:\n",
    "            if (not veganFriendly or i[2]) and i[3]<=maxPrice and i[4]<=maxDistance:\n",
    "                filter.append(i)\n",
    "        loc=list(range(len(filter)))\n",
    "        loc.sort(key=lambda x:(filter[x][1],filter[x][0]),reverse=True)\n",
    "        return [filter[i][0] for i in loc]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        res=[]\n",
    "        l=len(restaurants)\n",
    "        for i in range(l):\n",
    "            local=restaurants[i]\n",
    "            if local[2]>=veganFriendly and local[3]<=maxPrice and local[4]<=maxDistance:\n",
    "                res.append((local[1],local[0]))\n",
    "        res.sort(reverse=True)\n",
    "        sol=[res[i][1] for i in range(len(res))]\n",
    "        return sol\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        ret = []\n",
    "        for idi, ratingi, veganFriendlyi, pricei, distancei in restaurants:\n",
    "            if veganFriendly > veganFriendlyi or pricei > maxPrice or distancei > maxDistance:\n",
    "                continue\n",
    "            ret.append([ratingi, idi])\n",
    "        ret.sort(reverse=True)\n",
    "        return [idi for _, idi in ret]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        l1 = []\n",
    "        if veganFriendly == 1:\n",
    "            for i in range (len(restaurants)):\n",
    "                if restaurants[i][2] == 1:\n",
    "                    l1.append(restaurants[i])\n",
    "        else:\n",
    "            l1 = restaurants\n",
    "        l2 = []\n",
    "        for i in range (len(l1)):\n",
    "            if l1[i][3] <= maxPrice:\n",
    "                l2.append(l1[i])\n",
    "        l3 = []\n",
    "        for i in range (len(l2)):\n",
    "            if l2[i][4] <= maxDistance:\n",
    "                l3.append(l2[i])\n",
    "        l3 = sorted(l3, key = lambda x:(-x[1], -x[0]))\n",
    "        l4 = []\n",
    "        for i in range (len(l3)):\n",
    "            l4.append(l3[i][0])\n",
    "        return l4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        result_list = []\n",
    "        range_list = []\n",
    "        for res in restaurants:\n",
    "            if res[3] <= maxPrice and res[4] <= maxDistance:\n",
    "                if veganFriendly:\n",
    "                    if res[2] == 1:\n",
    "                        result_list.append(res)\n",
    "                else:\n",
    "                    result_list.append(res)\n",
    "        rat = sorted(result_list, key=lambda x: (-x[1],-x[0]))\n",
    "        range_list = [row[0] for row in rat]\n",
    "        return range_list\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        filterRest = []\n",
    "        for i, r, v, p, d in restaurants:\n",
    "            if ((veganFriendly == 1 and v==1) or (veganFriendly == 0)) and p <= maxPrice and d <= maxDistance:\n",
    "                filterRest.append([i,r])\n",
    "\n",
    "        filterRest.sort(key=lambda x:[-x[1],-x[0]])\n",
    "        return [i for i,_ in filterRest]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\r\n",
    "        ans = list()\r\n",
    "        for restaurant in restaurants:\r\n",
    "            if restaurant[2] >= veganFriendly and \\\r\n",
    "               restaurant[3] <= maxPrice and \\\r\n",
    "               restaurant[4] <= maxDistance:\r\n",
    "                ans.append(restaurant)\r\n",
    "        ans.sort(key=lambda x: (-x[1], -x[0]))\r\n",
    "        return [x[0] for x in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        filtered = list(filter(lambda r: (not veganFriendly or r[2]) and r[3] <= maxPrice and r[4] <= maxDistance, restaurants))\n",
    "        filtered.sort(reverse=True, key=lambda r: (r[1], r[0]))\n",
    "        return list(map(lambda r: r[0], filtered))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        filtered = [r for r in restaurants if r[3] <= maxPrice and r[4] <= maxDistance and r[2] >= veganFriendly]\n",
    "        filtered.sort(key=lambda r: (-r[1], -r[0]))\n",
    "        return [r[0] for r in filtered]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:\n",
    "        filtered = [ r for r in restaurants if not(veganFriendly and not r[2]) and r[3]<=maxPrice and r[4]<=maxDistance]\n",
    "        filtered.sort(key=lambda x:(x[1],x[0]),reverse=True)\n",
    "        return [r[0] for r in filtered]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
